package org.wzq.android_api;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONException;
import org.wzq.utils.AndroidUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取设备信息,注意一些权限
 */
public class Act_DeviceInfo extends Activity {
    Button btnGetInfo;
    TextView tvShowInfo;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_device_info);
        btnGetInfo = (Button) findViewById(R.id.btn_get_device_info);
        tvShowInfo = (TextView) findViewById(R.id.tv_show_device_info);


        btnGetInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String deviceInfo = getDeviceInfo();
                    tvShowInfo.setText(deviceInfo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * 获取设备详细信息
     */

    private String getDeviceInfo() throws JSONException {
        //JSONObject json = new JSONObject();
        MyJsonObject json = new MyJsonObject();
        // iphone特有 android没有的字段
        json.put("AvailableDeviceCapacity", "");//兼容iphone
        json.put("DeviceCapacity", "");//兼容iphone
        json.put("IsActivationLockEnabled", "");//兼容iphone
        json.put("IsCloudBackupEnabled", "");//兼容iphone
        json.put("IsDeviceLocatorServiceEnabled", "");//兼容iphone
        json.put("IsDoNotDisturbInEffect", "");//兼容iphone
        json.put("UDID", "");//TODO
        json.put("iTunesStoreAccountIsActive", "");


        // 都有的字段
        json.put("BatteryLevel", AndroidUtils.getBatteryInfo());// 电池
        json.put("BluetoothMAC", AndroidUtils.getBluetoothMac());// BluetoothMAC
        json.put("BuildVersion", Build.VERSION.RELEASE);
        json.put("DeviceName", Build.USER);
        json.put("EASDeviceIdentifier", AndroidUtils.getDeviceId());
        json.put("IsSupervised", AndroidUtils.isRoot());
        json.put("Model", Build.MODEL);
        json.put("ProductName", Build.PRODUCT);
        json.put("SerialNumber", Build.SERIAL);
        json.put("WiFiMAC", AndroidUtils.getWifiMac());

        // android 特有的字段
        json.put("AvailableInternalMemorySize", AndroidUtils.getAvailableInternalMemorySize());
        json.put("AvailableExternalMemorySize", AndroidUtils.getAvailableExternalMemorySize());
        json.put("TotalInternalMemorySize", AndroidUtils.getTotalInternalMemorySize());
        json.put("TotalExternalMemorySize", AndroidUtils.getTotalExternalMemorySize());

        // toString
        return json.toString();
    }

    static class MyJsonObject {
        private HashMap<String, String> map = new HashMap<String, String>();

        public MyJsonObject put(String key, String value) {
            map.put(key, value);

            return this;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                sb.append(key).append("=").append(value).append('\n');
            }
            return sb.toString();
        }
    }
}
